home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / SCHEME / GNU / SCM4E1 / !Scm / slib / logical < prev    next >
Text File  |  1993-04-02  |  5KB  |  143 lines

  1. ;;;; logical.scm, bit access and operations for integers for Scheme
  2. ;;; Copyright (C) 1991, 1993 Aubrey Jaffer.
  3.  
  4. ;Permission to copy this software, to redistribute it, and to use it
  5. ;for any purpose is granted, subject to the following restrictions and
  6. ;understandings.
  7.  
  8. ;1.  Any copy made of this software must include this copyright notice
  9. ;in full.
  10.  
  11. ;2.  I have made no warrantee or representation that the operation of
  12. ;this software will be error-free, and I am under no obligation to
  13. ;provide any services, by way of maintenance, update, or otherwise.
  14.  
  15. ;3.  In conjunction with products arising from the use of this
  16. ;material, there shall be no use of my name in any advertising,
  17. ;promotional, or sales literature without prior written consent in
  18. ;each case.
  19.  
  20. (define logical:integer-expt
  21.   (if (provided? 'inexact)
  22.       expt
  23.       (lambda (n k)
  24.     (logical:ipow-by-squaring n k 1 *))))
  25.  
  26. (define (logical:ipow-by-squaring x k acc proc)
  27.   (cond ((zero? k) acc)
  28.     ((= 1 k) (proc acc x))
  29.     (else (logical:ipow-by-squaring (proc x x)
  30.                     (quotient k 2)
  31.                     (if (even? k) acc (proc acc x))
  32.                     proc))))
  33.  
  34. (define (logical:logand n1 n2)
  35.   (cond ((= n1 n2) n1)
  36.     ((zero? n1) 0)
  37.     ((zero? n2) 0)
  38.     (else
  39.      (+ (* (logical:logand (logical:ash-4 n1) (logical:ash-4 n2)) 16)
  40.         (vector-ref (vector-ref logical:boole-and (modulo n1 16))
  41.             (modulo n2 16))))))
  42.  
  43. (define (logical:logior n1 n2)
  44.   (cond ((= n1 n2) n1)
  45.     ((zero? n1) n2)
  46.     ((zero? n2) n1)
  47.     (else
  48.      (+ (* (logical:logior (logical:ash-4 n1) (logical:ash-4 n2)) 16)
  49.         (- 15 (vector-ref (vector-ref logical:boole-and
  50.                       (- 15 (modulo n1 16)))
  51.                   (- 15 (modulo n2 16))))))))
  52.  
  53. (define (logical:logxor n1 n2)
  54.   (cond ((= n1 n2) 0)
  55.     ((zero? n1) n2)
  56.     ((zero? n2) n1)
  57.     (else
  58.      (+ (* (logical:logxor (logical:ash-4 n1) (logical:ash-4 n2)) 16)
  59.         (vector-ref (vector-ref logical:boole-xor (modulo n1 16))
  60.             (modulo n2 16))))))
  61.  
  62. (define (logical:lognot n) (- -1 n))
  63.  
  64. (define (logical:bit-extract n start end)
  65.   (logical:logand (- (logical:integer-expt 2 (- end start)) 1)
  66.           (logical:ash n (- start))))
  67.  
  68. (define (logical:ash int cnt)
  69.   (if (negative? cnt)
  70.       (let ((n (logical:integer-expt 2 (- cnt))))
  71.     (if (negative? int)
  72.         (+ -1 (quotient (+ 1 int) n))
  73.         (quotient int n)))
  74.       (* (logical:integer-expt 2 cnt) int)))
  75.  
  76. (define (logical:ash-4 x)
  77.   (if (negative? x)
  78.       (+ -1 (quotient (+ 1 x) 16))
  79.       (quotient x 16)))
  80.  
  81. (define (logical:logcount n)
  82.   (cond ((zero? n) 0)
  83.     ((negative? n) (logical:logcount (logical:lognot n)))
  84.     (else
  85.      (+ (logical:logcount (logical:ash-4 n))
  86.         (vector-ref '#(0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4)
  87.             (modulo n 16))))))
  88.  
  89. (define (logical:integer-length n)
  90.   (case n
  91.     ((0 -1) 0)
  92.     ((1 -2) 1)
  93.     ((2 3 -3 -4) 2)
  94.     ((4 5 6 7 -5 -6 -7 -8) 3)
  95.     (else (+ 4 (logical:integer-length (logical:ash-4 n))))))
  96.  
  97. (define logical:boole-xor
  98.  '#(#(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
  99.     #(1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14)
  100.     #(2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13)
  101.     #(3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12)
  102.     #(4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11)
  103.     #(5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10)
  104.     #(6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9)
  105.     #(7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8)
  106.     #(8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7)
  107.     #(9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6)
  108.     #(10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5)
  109.     #(11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4)
  110.     #(12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3)
  111.     #(13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2)
  112.     #(14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1)
  113.     #(15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0)))
  114.  
  115. (define logical:boole-and
  116.  '#(#(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
  117.     #(0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1)
  118.     #(0 0 2 2 0 0 2 2 0 0 2 2 0 0 2 2)
  119.     #(0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3)
  120.     #(0 0 0 0 4 4 4 4 0 0 0 0 4 4 4 4)
  121.     #(0 1 0 1 4 5 4 5 0 1 0 1 4 5 4 5)
  122.     #(0 0 2 2 4 4 6 6 0 0 2 2 4 4 6 6)
  123.     #(0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7)
  124.     #(0 0 0 0 0 0 0 0 8 8 8 8 8 8 8 8)
  125.     #(0 1 0 1 0 1 0 1 8 9 8 9 8 9 8 9)
  126.     #(0 0 2 2 0 0 2 2 8 8 10 10 8 8 10 10)
  127.     #(0 1 2 3 0 1 2 3 8 9 10 11 8 9 10 11)
  128.     #(0 0 0 0 4 4 4 4 8 8 8 8 12 12 12 12)
  129.     #(0 1 0 1 4 5 4 5 8 9 8 9 12 13 12 13)
  130.     #(0 0 2 2 4 4 6 6 8 8 10 10 12 12 14 14)
  131.     #(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)))
  132.  
  133. (define logand logical:logand)
  134. (define logior logical:logior)
  135. (define logxor logical:logxor)
  136. (define lognot logical:lognot)
  137. (define ash logical:ash)
  138. (define logcount logical:logcount)
  139. (define integer-length logical:integer-length)
  140. (define bit-extract logical:bit-extract)
  141. (define ipow-by-squaring logical:ipow-by-squaring)
  142. (define integer-expt logical:integer-expt)
  143.